import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Divider} from 'antd';
import {FILES_API, DATE_FORMAT} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {injectIntl} from './intl';
import {transportType, declarePlace, declareType} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';

const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    componentDidMount() {
        this.props.init();
    }

    render() {
        const {formatMessage} = this.props.intl;
        const detailData = this.props.detail || {};
        const {declareAttachments = [], clearanceAttachments = []} = detailData;

        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: '备件发运单详情'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandCode',
                            defaultMessage: '品牌编号'
                        })}>{this.props.detail.brandCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerCode',
                            defaultMessage: '总代编号'
                        })}>{this.props.detail.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '编号'
                        })}>{this.props.detail.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.invoicedDate',
                            defaultMessage: '开票日期'
                        })}>{formatDateTime(this.props.detail.invoicedDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.shipmentType',
                            defaultMessage: '发运方式'
                        })}>{conventEnumValueToString(transportType, this.props.detail.shipmentType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.origin',
                            defaultMessage: '起运地'
                        })}>{this.props.detail.origin}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.destination',
                            defaultMessage: '目的地'
                        })}>{this.props.detail.destination}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.expectDepartedDate',
                            defaultMessage: '预计离港日期'
                        })}>{formatDateTime(this.props.detail.expectDepartedDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lcCode',
                            defaultMessage: '信用证编号	'
                        })}>{this.props.detail.lcCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.declarePlace',
                            defaultMessage: '报关地点'
                        })}>{conventEnumValueToString(declarePlace, this.props.detail.declarePlace)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.declareType',
                            defaultMessage: '报关类型'
                        })}>{conventEnumValueToString(declareType, this.props.detail.declareType)}</Description>
                    </DescriptionList>
                
                    <Divider/>
                    {declareAttachments && declareAttachments.length > 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPage.card.declareAttachment',
                            defaultMessage: '报关附件清单'
                        })} size="large">
                            {declareAttachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">{item.fileName}</a></Description>))}
                        </DescriptionList>
                        : formatMessage({
                            id: 'detailPage.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}
                    <Divider/>
                    {clearanceAttachments && clearanceAttachments.length > 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPage.card.clearanceAttachment',
                            defaultMessage: '清关附件清单'
                        })} size="large">
                            {clearanceAttachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">{item.fileName}</a></Description>))}
                        </DescriptionList>
                        : formatMessage({
                            id: 'detailPage.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    getDetail,
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const getIsDealerUser = selectorFactory(['page', 'domainData', 'initData', 'isDealerUser']);

const mapStateToProps = (state, props) => ({
    id: props.id,
    isDealerUser: getIsDealerUser(state),
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
